
package com.admissions.tracnghiem;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.admissions.AdmissionsActivity;
import com.admissions.CallBack;
import com.admissions.R;
import com.admissions.TabGroupActivity;
import com.admissions.adapter.AllThreadsAdapter;
import com.admissions.model.CauHoi;
import com.admissions.model.DeThi;
import com.admissions.network.DataHelper;
import com.admissions.network.HttpHelper;
import com.admissions.network.URL;
import com.admissions.utils.JSONHelper;

/**
 * @author Bkav-DangNVb
 *
 */
public class AllThreadsActivity extends ListActivity implements CallBack {
    private Resources mRes;

    private ListView mListView;

    private DeThi mDeThi;

    private ArrayList<CauHoi> questions = new ArrayList<CauHoi>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_threads);
        mRes = getResources();

        mListView = getListView();
        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout footer = (LinearLayout) inflater
                .inflate(R.layout.listview_footer, null, false);
        mListView.addFooterView(footer);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Intent intent = getIntent();
        Bundle bS = intent.getExtras();
        mDeThi = (DeThi) bS.getSerializable(DeThi.DETHI);

        // Setup Header
        String headerContent = mDeThi.name;
        String headerComment = mRes.getString(R.string.mon_thi_caption);
        Drawable icon = null;

        TabGroupActivity parent = (TabGroupActivity) getParent();
        AdmissionsActivity grandParent = (AdmissionsActivity) parent.getParent();
        grandParent.setupHeader(icon, headerContent, headerComment);

        getData();
    }

    public void bindData() {
        AllThreadsAdapter adapter = new AllThreadsAdapter(this, questions);
        mListView.setAdapter(adapter);
    }

    private void getData() {
        DataHelper task = new DataHelper(this);
        task.execute(new Void[] {});
    }

    @Override
    public void start() {

    }

    @Override
    public void doWorks() {
        String url = URL.URL_CAUHOI + mDeThi.id;
        String jsonString = HttpHelper.getJsonData(url);
        questions = JSONHelper.getAllQuestions(jsonString);
    }

    @Override
    public void onFinish() {
        bindData();
    }
}
